package com.github.llyb120.qiuyasql.driver;

import cn.hutool.core.util.StrUtil;
import com.github.llyb120.qiuyasql.entity.FieldType;
import com.github.llyb120.qiuyasql.entity.TableEntity;
import com.github.llyb120.qiuyasql.entity.TableField;
import com.github.llyb120.qiuyasql.entity.TablePatch;
import lombok.var;

import java.util.List;

/**
 * @Author: Administrator
 * @Date: 2020/10/7 18:01
 */
public class MysqlDriver implements SqlDriver{


    public String buildCreateTableSql(TableEntity entity){
        /**
         *
         * CREATE TABLE `user` (
         *   `id` varchar(50) NOT NULL,
         *   `name` varchar(255) DEFAULT NULL,
         *   `age` int(10) DEFAULT '0',
         *   PRIMARY KEY (`id`)
         * ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
         *
         */
        StringBuilder sb = new StringBuilder();
        sb.append("create table ");
        sb.append("`");
        sb.append(entity.getTable());
        sb.append("` (");
        for (TableField field : entity.getFields()) {
            sb.append("`");
            sb.append(field.getName());
            sb.append("`");
            if (field.getName().equals("id")) {
                sb.append(" varchar(50) not null");
            } else {
                sb.append(getTypeSql(field.getType()));
            }
            sb.append(",");
        }
        //primary
        sb.append("primary key (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
        return sb.toString();
    }

    @Override
    public String buildAlterTableColumnSql(TableEntity tableEntity, TablePatch tablePatch){
        var sb = new StringBuilder();
        sb.append("alter table `");
        sb.append(tableEntity.getTable());
        sb.append("`");
        for (TableField tableField : tablePatch.getLessFields()) {
            sb.append(" add column `");
            sb.append(tableField.getName());
            sb.append("`");
            sb.append(getTypeSql(tableField.getType()));
            sb.append(",");
        }
        for (TableField changedField : tablePatch.getChangedFields()) {
            sb.append(" modify column `");
            sb.append(changedField.getName());
            sb.append("`");
            sb.append(getTypeSql(changedField.getType()));
            sb.append(",");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    public String getTypeSql(FieldType type){
        switch (type) {
            case STRING:
                return " varchar(255) default null";//sb.append(" varchar(255) default ''");
            case DECIMAL:
                return " decimal(10,2) default null";//sb.append(" decimal(10,2) default 0");
        }
        return "";
    }

    public FieldType getTypeFromField(String type) {
        if (StrUtil.containsIgnoreCase(type, "varchar")) {
            return FieldType.STRING;
        } else if (StrUtil.containsIgnoreCase(type, "decimal")) {
            return FieldType.DECIMAL;
        } else {
            return null;
        }
    }

    @Override
    public boolean hasWrongTable(Throwable throwable) {
        return throwable.getMessage().contains("doesn't exist");
    }

    @Override
    public boolean hasWrongColumn(Throwable throwable) {
        return throwable.getMessage().contains("Unknown column") || throwable.getMessage().contains("Incorrect") || throwable.getMessage().contains("truncation");
    }
}
